package com.chezhujie.www.controller;

import com.chezhujie.www.constant.PageInfo;
import com.chezhujie.www.domain.Czjtemplate;
import com.chezhujie.www.service.CzjtemplateService;
import com.sys.core.page.Pagination;
import com.sys.core.utils.ConvertObj;
import com.sys.core.utils.UUIDUtils;
import com.sys.user.base.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/template")
public class CzjtemplateController extends BaseController {
	private static final Logger log = LoggerFactory
			.getLogger(CzjtemplateController.class);

	public CzjtemplateController() {
	}

	/**
	 * 接口 详情 返回详情 根据类型
	 * 
	 * @param templateId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("detailByType")
	public void detailByid(java.lang.String templateId,
			@RequestParam(required = true) Integer type,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {

		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("type", type);
			Czjtemplate czjtemplate = czjtemplateService.getTemplateByType(map);
			jsonResult(1, "", "", czjtemplate, response);
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult(0, "124", "首页六个模块广告查询出错", "", response);
		}
	}

	/**
	 * 模板广告(czj_template)删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("czjtemplateDelete")
	public String delete(@RequestParam("ids") List<String> templateIdList,
			HttpServletRequest request, HttpServletResponse response) {

		try {
			czjtemplateService.deleteById(templateIdList);
		} catch (Exception e) {
			log.error("error", e);
			e.printStackTrace();
		}
		return "redirect:/template/czjtemplateList";
	}

	/**
	 * 模板广告(czj_template)详情
	 * 
	 * @param templateId
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjtemplateDetail")
	public String detail(java.lang.String templateId,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {

		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("templateId", templateId);

			Czjtemplate czjtemplate = czjtemplateService.getTemplateById(map);
			model.addAttribute("czjtemplate", czjtemplate);
		} catch (Exception e) {
			log.error("error", e);
			e.printStackTrace();

		}
		return "czjtemplate/czjtemplate_detail";
	}

	/**
	 * 模板广告(czj_template)查询（分页）
	 * 
	 * @param czjtemplate
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("czjtemplateList")
	public String listPage(Czjtemplate czjtemplate, Integer pageNo,
			Integer pageSize, String order, String sort,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {

		try {
			if (null == pageNo)
				pageNo = PageInfo.DEFAULT_PAGENO;
			if (null == pageSize)
				pageSize = PageInfo.DEFAULT_PAGESIZE;
			Pagination pagination = czjtemplateService.getCzjtemplateList(
					ConvertObj.toMap(czjtemplate), pageSize, pageNo);
			model.addAttribute("pagination", pagination);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("error", e);
		}

		return "czjtemplate/czjtemplate_list";

	}

	/**
	 * 模板广告(czj_template) 修改页面
	 * 
	 * @param czjtemplate
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("goupdate")
	public String goupdate(String templateId, Integer pageNo, Integer pageSize,
			String order, String sort, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {

		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("templateId", templateId);
			Czjtemplate czjtemplate = czjtemplateService.getTemplateById(map);
			model.addAttribute("czjtemplate", czjtemplate);

		} catch (Exception e) {
			e.printStackTrace();
			log.error("error", e);
		}

		return "czjtemplate/temelate_update";

	}

	/**
	 * 模板广告(czj_template)修改
	 * 
	 * @param czjtemplate
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("doupdate")
	public String doupdate(
			Czjtemplate czjtemplate,
			Integer pageNo,
			Integer pageSize,
			String order,
			@RequestParam(value = "file", required = false) MultipartFile[] file,
			String sort, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {

		try {
			String pathRoot = request.getSession().getServletContext()
					.getRealPath("");

			for (int i = 0; i < file.length; i++) {
				String path = "/upload/" + UUIDUtils.getUUID() + ".jpg";// 数据库保存路径

				if(i==0 && file[i].getSize()>0){
					
					czjtemplate.setPicPath(path);
				}
				if(i==1 && file[i].getSize()>1){
					
					czjtemplate.setBackgroundPath(path);
				}
				
				File fileDir = new File(pathRoot + "/upload");
				if (!fileDir.exists()) {
					fileDir.mkdir();
				}
				file[i].transferTo(new File(pathRoot + path));

			}
			if (null == czjtemplate.getTemplateId()
					|| "".equals(czjtemplate.getTemplateId())) { // 新增

			} else { // 修改

				czjtemplateService.updateTemplate(czjtemplate);
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("error", e);
		}

		return "redirect:/template/czjtemplateList";

	}

	@Resource
	private CzjtemplateService czjtemplateService;

}